package Thread;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2024/08/13
 * 线程争抢
 */
public class ThreadContention {
    private static int COUNT = 0;

    public synchronized  static void adder(){
        COUNT++;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                adder();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                adder();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("最后的结果是："+COUNT);
    }


    /*
    不加synchronized关键字的时候
    理想结果：20000
    结果：18574
    20000
    19731
     */
}
